
package oglTest;

import javax.media.opengl.GL;
//import javax.media.opengl.GL2;
//import javax.media.opengl.GL2ES1;
//import javax.media.opengl.GLAutoDrawable;
//import javax.media.opengl.GLEventListener;
//import javax.media.opengl.awt.GLCanvas;
//import javax.media.opengl.fixedfunc.GLLightingFunc;
//import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
import allparser.*;
import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureData;
import com.sun.opengl.util.texture.TextureIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class AllDraw extends AllFile {
    public AllDraw(int poczatek, int ilosc_datagramow, String filename) throws IOException {
        super(poczatek, ilosc_datagramow, filename);
    }
    
    public int akti = 0;
    public String path;
    private Texture wTexture;
    public boolean jestawc = true;
    public float skok = 0.5f;
    public float zakres = 200;
    
    public void DrawAllLines(final GL gl, GLU glu, boolean animacja, float odstep){
        float x = 0.0f;
        
        //for(Datagram68 dt : this.datagram68){
        int pocz = akti-250;
        if(pocz<0) pocz = 0;
        x = 0 - odstep*(akti-pocz);
        
        int w74 = 12;
        
        int rakti = akti + 200;
        if(rakti>=this.datagram68.size())
            rakti = this.datagram68.size() - 1;
        
        for(; pocz< rakti; pocz++){
        Datagram68 dt = this.datagram68.get(pocz); 
        
            x = x + (float)odstep;

            for(int i = 0; i < dt.beamCount-1; i++){
                 gl.glBegin(GL.GL_LINES);
                    gl.glColor3f((float)(1 - (dt.Z[i]-w74)/10), (float)(0 + (dt.Z[i]-w74)/12), (float)0);

                    gl.glVertex3f(x, -(float)dt.Z[i], (float)dt.Y[i]/2);
                    gl.glColor3f((float)(1 - (dt.Z[i+1]-w74)/10), (float)(0 + (dt.Z[i+1]-w74)/12), (float)0);
                    gl.glVertex3f(x, -(float)dt.Z[i+1], (float)dt.Y[i+1]/2);
                gl.glEnd();
            }
        }
        Datagram68 dt = this.datagram68.get(akti); 
        if(jestawc){
            x = x - odstep*(rakti-akti);
            
            int l = dt.Z.length/2;

            gl.glEnable(GL.GL_TEXTURE_2D);
            gl.glEnable(GL.GL_BLEND);

            if(new File(path+".cache/"+akti+".png").exists()){
                try {
                    InputStream stream = new FileInputStream(path+".cache/"+akti+".png");
                    TextureData data = TextureIO.newTextureData(stream, false, "png");
                    wTexture = TextureIO.newTexture(data);
                }
                catch (IOException exc) {
                    exc.printStackTrace();
                    System.exit(1);
                }
            
                wTexture.enable();
                wTexture.bind();
            }

            gl.glBegin(GL.GL_QUADS);
               gl.glColor3f(2, 2, 2);
               gl.glTexCoord2f(1, 0);
               gl.glVertex3f(x, 0 , (float)dt.Y[0]*1.1f/2);
               gl.glColor3f(2, 2, 2);
               gl.glTexCoord2f(0, 0);
               gl.glVertex3f(x, 0 ,(float)dt.Y[dt.Y.length-1]*1.1f/2);
               gl.glColor3f(2, 2, 2);
               gl.glTexCoord2f(0, 1);
               gl.glVertex3f(x, -(float)dt.Z[0]*1.3f, (float)dt.Y[dt.Y.length-1]*1.1f/2);
               gl.glColor3f(2, 2, 2);
               gl.glTexCoord2f(1, 1);
               gl.glVertex3f(x , -(float)dt.Z[0]*1.3f , (float)dt.Y[0]*1.1f/2);
            gl.glEnd();
            
            gl.glDisable(GL.GL_BLEND);
        }
        drawShip(gl, (float)(dt.Y[0]/2 + dt.Y[dt.Y.length-1]/2)/2);
    }
    static void drawShip(final GL gl, float offset)
    {
      gl.glDisable(GL.GL_TEXTURE_2D);
      //wizualziacja statku
      gl.glBegin(GL.GL_POLYGON);/* f1: front */
        gl.glColor3f(0.4f, 0.4f, 0.4f);
        gl.glNormal3f(-1.0f,0.0f,0.0f);
        gl.glVertex3f(-1.0f,0.0f,-1.0f+offset);
        gl.glVertex3f(-1.0f,0.0f,1.0f+offset);
        gl.glVertex3f(1.0f,0.0f,1.0f+offset);
        gl.glVertex3f(1.0f,0.0f,-1.0f+offset);
      gl.glEnd();
      gl.glBegin(GL.GL_POLYGON);/* f2: bottom */
        gl.glColor3f(0.4f, 0.4f, 0.4f);  
        gl.glNormal3f(0.0f,0.0f,-1.0f);
        gl.glVertex3f(-1.0f,0.0f,-1.0f+offset);
        gl.glVertex3f(1.0f,0.0f,-1.0f+offset);
        gl.glVertex3f(1.0f,1.0f,-1.0f+offset);
        gl.glVertex3f(-1.0f,1.0f,-1.0f+offset);
      gl.glEnd();
      gl.glBegin(GL.GL_POLYGON);/* f3:back */
        gl.glColor3f(0.4f, 0.4f, 0.4f);
        gl.glNormal3f(1.0f,0.0f,0.0f);
        gl.glVertex3f(1.0f,1.0f,-1.0f+offset);
        gl.glVertex3f(1.0f,1.0f,1.0f+offset);
        gl.glVertex3f(-1.0f,1.0f,1.0f+offset);
        gl.glVertex3f(-1.0f,1.0f,-1.0f+offset);
      gl.glEnd();
      gl.glBegin(GL.GL_POLYGON);/* f4: top */
        gl.glColor3f(0.4f, 0.4f, 0.4f);
        gl.glNormal3f(0.0f,0.0f,1.0f);
        gl.glVertex3f(1.0f,1.0f,1.0f+offset);
        gl.glVertex3f(1.0f,0.0f,1.0f+offset);
        gl.glVertex3f(-1.0f,0.0f,1.0f+offset);
        gl.glVertex3f(-1.0f,1.0f,1.0f+offset);
      gl.glEnd();
      gl.glBegin(GL.GL_POLYGON);/* f5: left */
        gl.glColor3f(0.4f, 0.4f, 0.4f);
        gl.glNormal3f(0.0f,1.0f,0.0f);
        gl.glVertex3f(-1.0f,0.0f,-1.0f+offset);
        gl.glVertex3f(-1.0f,1.0f,-1.0f+offset);
        gl.glVertex3f(-1.0f,1.0f,1.0f+offset);
        gl.glVertex3f(-1.0f,0.0f,1.0f+offset);
      gl.glEnd();
      gl.glBegin(GL.GL_POLYGON);/* f6: right */
        gl.glColor3f(0.4f, 0.4f, 0.4f);
        gl.glNormal3f(0.0f,-1.0f,0.0f);
        gl.glVertex3f(1.0f,0.0f,-1f+offset);
        gl.glVertex3f(1.0f,0.0f,1.0f+offset);
        gl.glVertex3f(1.0f,1.0f,1.0f+offset);
        gl.glVertex3f(1.0f,1.0f,-1.0f+offset);
      gl.glEnd();
    }
}
